package cos.android.voleysplash.viewythread;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Vector;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import cos.android.voleysplash.R;
import cos.android.voleysplash.element.Element;
import cos.android.voleysplash.level.Level;
import cos.android.voleysplash.level.LevelBricks;

public class PlaygroundThread extends Thread {

	private Bitmap background;

	private Resources res;

	private int canvasHeight = 1;
	private int canvasWidth = 1;

	int level;
	int world;
	Level level1;

	private Context context;
	private Properties properties;
	private boolean mRun;

	private boolean running = false;

	private long mLastTime;

	/** Handle to the surface manager object we interact with */
	private SurfaceHolder mSurfaceHolder;

	public PlaygroundThread(SurfaceHolder surfaceHolder, Context context,
			Handler handler, int level, int world) {
		mSurfaceHolder = surfaceHolder;

		mLastTime = System.currentTimeMillis() + 10;
		this.context = context;

		inizialize(level, world);

	}

	private void inizialize(int level, int world) {
		res = context.getResources();
		this.level = level;
		this.world = world;
		InputStream rawResource = res.openRawResource(R.raw.settings);
		properties = new Properties();
		try {
			properties.load(rawResource);
		} catch (IOException e) {

			e.printStackTrace();
		}

		level1 = new Level(res, properties, level, world, canvasHeight,
				canvasWidth);
		System.out.println("Hola2");

		// level1.basicInizialize(res, properties);

		background = BitmapFactory.decodeResource(res, R.drawable.mainfield);
		if ((canvasHeight <= 10) || (canvasWidth <= 10)) {

		} else {
			setSurfaceSize(canvasWidth, canvasHeight);
		}

	}

	@Override
	public void run() {
		while (mRun) {
			Canvas c = null;
			try {
				c = mSurfaceHolder.lockCanvas(null);
				synchronized (mSurfaceHolder) {
					if (running) {
						update(c);
					}
					doDraw(c);
				}
			} finally {
				// do this in a finally so that if an exception is thrown
				// during the above, we don't leave the Surface in an
				// inconsistent state
				if (c != null) {
					mSurfaceHolder.unlockCanvasAndPost(c);
				}
			}
		}
	}

	private void update(Canvas canvas) {
		long now = System.currentTimeMillis();

		// Do nothing if mLastTime is in the future.
		// This allows the game-start to delay the start of the physics
		// by 100ms or whatever.
		if (mLastTime > now)
			return;

		/*
		 * if (level1.checkGoal()) { if (level != 5) { inizialize(level + 1,
		 * world); running = false; } else { inizialize(1, world + 1); running =
		 * false; } }
		 */

		float elapsed = (now - mLastTime) / 1000.0f;

		// checkStatus();

		level1.updateGame(elapsed, canvas);

		mLastTime = now;

	}

	private void doDraw(Canvas canvas) {

		canvas.drawBitmap(background, 0, 0, null);

		Vector<Element> elements = level1.getElements();
		for (int j = 0; j < elements.size(); j++) {

			Element element = elements.elementAt(j);
			drawElement(canvas, element);
		}
	}

	private void drawElement(Canvas canvas, Element element) {

		if (element.getSprite() != null) {
			canvas.drawBitmap(element.getSprite(),
					Math.round(element.getxPos()),
					Math.round(element.getyPos()), null);
		}

	}

	public void setSurfaceSize(int width, int height) {
		synchronized (mSurfaceHolder) {
			canvasWidth = width;
			canvasHeight = height;

			// don't forget to resize the background image
			System.out.println("width: " + width + " " + height);
			background = Bitmap.createScaledBitmap(background, canvasWidth,
					canvasHeight, false);
			level1.setCanvasHeight(height);
			level1.setCanvasWidth(width);
			// play = Bitmap.createScaledBitmap(play, (int) (canvasHeight *
			// 0.125), (int) (canvasHeight * 0.125),false);
			// save = Bitmap.createScaledBitmap(save, (int) (canvasHeight *
			// 0.1),(int) (canvasHeight * 0.1), false);
			/*
			 * level1.resizeElements((double) canvasWidth / 480d, (double)
			 * canvasHeight / 295d);
			 */
		}

	}

	public void setRunning(boolean bool) {
		mRun = bool;
	}

	public void reset() {
		// level1.resetLevel();

	}

	public boolean doTouchEvent(MotionEvent event) {

		float touchY;
		float touchX;

		if (event.getAction() == MotionEvent.ACTION_DOWN) {

			if (running == false) {
				running = true;
				mLastTime = System.currentTimeMillis() + 10;

			} else {
				touchY = event.getY();
				touchX = event.getX();

				if (touchY > canvasHeight * 0.6) {

					level1.hitBall(touchX, touchY);

				}
			}
		}
		return true;
	}
}
